<?php
namespace app\interfaces\controller;

use app\interfaces\model\MemberWithdraw as MemberWithdrawModel;
use app\interfaces\model\MemberBrokerage as MemberBrokerageModel;
use app\interfaces\model\Member as MemberModel;
use think\Request;
use think\Db;
use think\Exception;

class MemberWithdraw
{
    /*
     * 用户端 - 明细
     * @param  string $user_id  会员ID
     * @param  string $type     分类
     */
    public function withdraw_list()
    {
        // 获取参数
        $user_id = Request::instance()->param('user_id');
        $type = Request::instance()->param('type', '0');

        if (empty($user_id)) throw new Exception('not Empty');

        // 搜索条件
        $condition['member_id'] = ['eq', $user_id];
        $condition['status'] = ['eq', '1'];

        if (!empty($type)) $condition['type_id'] = ['eq', $type];
        if (empty($type)) $condition['type_id'] = ['neq', $type];

        // 展示数据
        $model = new MemberWithdrawModel();

        $result = $model->where($condition)->field('id,type_id,price,balance,create_time')->order(['create_time' => 'desc'])->paginate(10);

        return ['code' => 200, 'message' => '成功', 'data' => $result];

    }

    /*
     * 生成订单号码
     */
    public function createNumber()
    {
        //订单号码主体（YYYYMMDDHHIISSNNNNNNNN）
        $order_id_main = date('YmdHis') . rand(10000000, 99999999);
        //订单号码主体长度
        $order_id_len = strlen($order_id_main);
        $order_id_sum = 0;
        for ($i = 0; $i < $order_id_len; $i++) {
            $order_id_sum += (int)(substr($order_id_main, $i, 1));
        }
        //唯一订单号码（YYYYMMDDHHIISSNNNNNNNNCC）
        $order_id = $order_id_main . str_pad((100 - $order_id_sum % 100) % 100, 2, '0', STR_PAD_LEFT);

        return ['code' => 200, 'message' => '成功', 'data' => $order_id];
    }

}
